<template>
  <div class="tabBar">
    <van-tabbar route v-model="activeIndex" active-color="var(--parmary-color)">
      <van-tabbar-item
        icon="home-o"
        :to="item.path"
        v-for="(item, index) in tabBarInfo"
        :key="index"
      >
        <span>{{ item.name }}</span>
        <template #icon>
          <img
            v-if="index != activeIndex"
            :src="getAssetURL(item.iconUrl)"
            alt=""
          />
          <img v-else :src="getAssetURL(item.activeIconURL)" alt="" />
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup>
import { getAssetURL } from "../../utils/load_assets";
import tabBarInfo from "@/assets/data/tabBar";
import { ref, watch } from "vue";
import { useRoute } from "vue-router";

const activeIndex = ref(0);
const route = useRoute();
watch(route, (newRoute) => {
  const index = tabBarInfo.findIndex((item) => {
    return item.path === newRoute.path;
  });
  if (index === -1) return;
  activeIndex.value = index;
});
</script>

<style lang="less" scoped></style>
